package com.ruoyi.ucar.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.ucar.boreq.ContractToolDetailQueryBoReq;
import com.ruoyi.ucar.boreq.ContractToolListQueryBoReq;
import com.ruoyi.ucar.boresp.*;
import com.ruoyi.ucar.domain.TContractTool;
import com.ruoyi.ucar.domain.po.ContractToolDetailQueryPo;
import com.ruoyi.ucar.domain.po.ContractToolListQueryPo;
import com.ruoyi.ucar.mapper.TContractToolMapper;
import com.ruoyi.ucar.moreq.ContractToolDetailQueryMoReq;
import com.ruoyi.ucar.moreq.ContractToolListQueryMoReq;
import com.ruoyi.ucar.service.ISysFileMainService;
import com.ruoyi.ucar.service.ITContractToolService;
import com.ruoyi.ucar.util.CommonUtil;
import com.ruoyi.ucar.voreq.ContractToolAddVoReq;
import com.ruoyi.ucar.voreq.ContractToolUpdateVoReq;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 合同工具Service业务层处理
 * 
 * @author sungangbin
 * @date 2025-05-13
 */
@Service
public class TContractToolServiceImpl extends ServiceImpl<TContractToolMapper, TContractTool> implements ITContractToolService
{
    @Autowired
    private TContractToolMapper tContractToolMapper;

    @Autowired
    private ISysFileMainService iSysFileMainService;

    /**
     * 查询合同工具
     * 
     * @param id 合同工具主键
     * @return 合同工具
     */
    @Override
    public TContractTool selectTContractToolById(Long id)
    {
        return tContractToolMapper.selectTContractToolById(id);
    }

    /**
     * 查询合同工具列表
     * 
     * @param tContractTool 合同工具
     * @return 合同工具
     */
    @Override
    public List<TContractTool> selectTContractToolList(TContractTool tContractTool)
    {
        return tContractToolMapper.selectTContractToolList(tContractTool);
    }

    /**
     * 新增合同工具
     * 
     * @param tContractTool 合同工具
     * @return 结果
     */
    @Override
    public int insertTContractTool(TContractTool tContractTool)
    {
        return tContractToolMapper.insertTContractTool(tContractTool);
    }

    /**
     * 修改合同工具
     * 
     * @param tContractTool 合同工具
     * @return 结果
     */
    @Override
    public int updateTContractTool(TContractTool tContractTool)
    {
        return tContractToolMapper.updateTContractTool(tContractTool);
    }

    /**
     * 批量删除合同工具
     * 
     * @param ids 需要删除的合同工具主键
     * @return 结果
     */
    @Override
    public int deleteTContractToolByIds(Long[] ids)
    {
        return tContractToolMapper.deleteTContractToolByIds(ids);
    }

    /**
     * 删除合同工具信息
     * 
     * @param id 合同工具主键
     * @return 结果
     */
    @Override
    public int deleteTContractToolById(Long id)
    {
        return tContractToolMapper.deleteTContractToolById(id);
    }

    @Override
    public String saveContractTool(ContractToolAddVoReq contractToolAddVoReq) throws IOException {
        TContractTool tContractTool= CommonUtil.objToObj(contractToolAddVoReq,TContractTool.class);
        String sysFileMainUuid=iSysFileMainService.saveSysFile(contractToolAddVoReq.getFile());
        String contractToolUuid=CommonUtil.createUuid();
        tContractTool.setContractToolUuid(contractToolUuid);
        tContractTool.setFileMainUuid(sysFileMainUuid);
        tContractTool.setDeleteFlag(1L);
        tContractTool.setCreatedBy(SecurityUtils.getUsername());
        tContractToolMapper.insert(tContractTool);
        return contractToolUuid;
    }

    @Override
    public int updateContractTool(ContractToolUpdateVoReq contractToolUpdateVoReq) throws IOException {
        TContractTool tContractTool=this.selectTContractToolByUuid(contractToolUpdateVoReq.getContractToolUuid());
        tContractTool.setToolName(contractToolUpdateVoReq.getToolName());
        tContractTool.setRemark(contractToolUpdateVoReq.getRemark());
        tContractTool.setStatus(contractToolUpdateVoReq.getStatus());
        if(StringUtils.isNotEmpty(contractToolUpdateVoReq.getFileMainUuid())){
            tContractTool.setDeleteFlag(1L);
            tContractTool.setUpdatedBy(SecurityUtils.getUsername());
        }else {
            String sysFileMainUuid = iSysFileMainService.saveSysFile(contractToolUpdateVoReq.getFile());
            tContractTool.setFileMainUuid(sysFileMainUuid);
            tContractTool.setDeleteFlag(1L);
            tContractTool.setUpdatedBy(SecurityUtils.getUsername());
        }
        return tContractToolMapper.update(tContractTool,new LambdaQueryWrapper<TContractTool>().eq(TContractTool::getContractToolUuid,contractToolUpdateVoReq.getContractToolUuid()).eq(TContractTool::getDeleteFlag,1l));

    }

    @Override
    public int deletetTContractToolById(String contractToolUuid,Long id)
    {
        TContractTool tContractTool=null;
        if(StringUtils.isNotEmpty(contractToolUuid)) {
            tContractTool = this.selectTContractToolByUuid(contractToolUuid);
            tContractTool.setDeleteFlag(-1l);
            tContractTool.setUpdatedBy(SecurityUtils.getUsername());
            return tContractToolMapper.update(tContractTool,new LambdaQueryWrapper<TContractTool>().eq(TContractTool::getContractToolUuid, contractToolUuid));
        }else{
            tContractTool = this.getById(id);
            tContractTool.setDeleteFlag(-1l);
            tContractTool.setUpdatedBy(SecurityUtils.getUsername());
            return tContractToolMapper.update(tContractTool,new LambdaQueryWrapper<TContractTool>().eq(TContractTool::getId, id));
        }

    }

    @Override
    public ContractToolDetailQueryBoResp contractToolDetailQuery(ContractToolDetailQueryBoReq contractToolDetailQueryBoReq) {
        ContractToolDetailQueryMoReq contractToolDetailQueryMoReq=CommonUtil.objToObj(contractToolDetailQueryBoReq,ContractToolDetailQueryMoReq.class);
        contractToolDetailQueryMoReq.setDeleteFlag(1l);
        List<ContractToolDetailQueryPo> contractToolDetailQueryPos= tContractToolMapper.contractToolDetailQuery(contractToolDetailQueryMoReq);
        if(StringUtils.isNotNull(contractToolDetailQueryPos)&&contractToolDetailQueryPos.size()>0){
            ContractToolDetailQueryPo contractToolDetailQueryPo= contractToolDetailQueryPos.get(0);
            ContractToolDetailQueryBoResp contractToolDetailQueryBoResp=CommonUtil.objToObj(contractToolDetailQueryPo,ContractToolDetailQueryBoResp.class);
            contractToolDetailQueryBoResp.setStatusDict(DictUtils.getDictLabel("common_status",contractToolDetailQueryPo.getStatus().toString()));
            return contractToolDetailQueryBoResp;
        }else{
            return null;
        }
    }

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public ContractToolListQueryBoResp contractToolListMobileQuery( ) {
        ContractToolListQueryBoResp contractToolListQueryBoResp=new ContractToolListQueryBoResp();
        List<ContractToolListQueryBo> contractToolListQueryBos=new ArrayList<>();
        ContractToolListQueryMoReq contractToolListQueryMoReq=new ContractToolListQueryMoReq();
        //删除标志
        contractToolListQueryMoReq.setDeleteFlag(1L);
        List<ContractToolListQueryPo> contractToolListQueryPoList = tContractToolMapper.contractToolListMobileQuery(contractToolListQueryMoReq);
        contractToolListQueryPoList.forEach(contractToolListQueryPo -> {
            ContractToolListQueryBo contractToolListQueryBo=CommonUtil.objToObj(contractToolListQueryPo,ContractToolListQueryBo.class);
            contractToolListQueryBo.setStatusDict(DictUtils.getDictLabel("common_status",contractToolListQueryPo.getStatus().toString()));
            contractToolListQueryBos.add(contractToolListQueryBo);
        });
        contractToolListQueryBoResp.setContractToolListQueryBos(contractToolListQueryBos);
        logger.info("轮播图查询BO端返回结果：{}"+contractToolListQueryBoResp);
        return contractToolListQueryBoResp;
    }


    @Override
    public ContractToolListQueryBoResp queryContractToolByCond(ContractToolListQueryBoReq contractToolListQueryBoReq) {
        ContractToolListQueryBoResp contractToolListQueryBoResp=new ContractToolListQueryBoResp();
        List<ContractToolListQueryBo> contractToolListQueryBos=new ArrayList<>();
        Page<ContractToolListQueryPo> page = new Page<>(contractToolListQueryBoReq.getPageIndex(),contractToolListQueryBoReq.getPageSize());
        ContractToolListQueryMoReq contractToolListQueryMoReq=CommonUtil.objToObj(contractToolListQueryBoReq,ContractToolListQueryMoReq.class);
        //删除标志
        contractToolListQueryMoReq.setDeleteFlag(1L);
        IPage<ContractToolListQueryPo> contractToolListQueryPage = tContractToolMapper.contractToolListQueryPage(page, contractToolListQueryMoReq);
        contractToolListQueryBoResp.setTotalCount(new Integer(String.valueOf(contractToolListQueryPage.getTotal())));
        contractToolListQueryBoResp.setTotalPage(new Integer(String.valueOf(contractToolListQueryPage.getPages())));
        contractToolListQueryPage.getRecords().forEach(contractToolListQueryPo -> {
            ContractToolListQueryBo contractToolListQueryBo=CommonUtil.objToObj(contractToolListQueryPo,ContractToolListQueryBo.class);
            contractToolListQueryBo.setStatusDict(DictUtils.getDictLabel("common_status",contractToolListQueryPo.getStatus().toString()));
            contractToolListQueryBos.add(contractToolListQueryBo);
        });
        contractToolListQueryBoResp.setContractToolListQueryBos(contractToolListQueryBos);
        logger.info("轮播图查询BO端返回结果：{}"+contractToolListQueryBoResp);
        return contractToolListQueryBoResp;
    }

    @Override
    public TContractTool selectTContractToolByUuid(String contractToolUuid) {
        return tContractToolMapper.selectOne(new LambdaQueryWrapper<TContractTool>().eq(TContractTool::getContractToolUuid, contractToolUuid).eq(TContractTool::getDeleteFlag,1l));
    }
}
